package shooter;

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * GUI is the provides the frame of the game. 
 * 
 * @author Michail Tausch
 */
public class GUI implements WindowListener {
	JFrame frame;
	JPanel panel;
	Canvas canvas;
	
	Shooter shooter = null;
	//Nice fancy object for using double buffering (i.e. prevent flickering).
	BufferStrategy strategy;
	
	/**
	 * Creates a new GUI object.
	 * @param width The game width.
	 * @param height The game height.
	 */
	public GUI(int width, int height, Shooter shooter){
		this.shooter = shooter;
		
		frame = new JFrame("C R O S S F I R E 2 D");
		JPanel contentPanel = (JPanel) frame.getContentPane();
		contentPanel.setPreferredSize(new Dimension(width, height));
//		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		panel = new JPanel(); //new JPanel(true) for DoubleBuffering
		panel.setLayout(null);
		contentPanel.add(panel);
		
		canvas = new Canvas();
		canvas.setBounds(0,0,width,height);
		panel.add(canvas);
		
		canvas.setIgnoreRepaint(false);
		
		frame.pack();
		frame.setVisible(true);
		frame.setResizable(false);
		frame.addWindowListener(this);
		
		frame.setLocationRelativeTo(null);
		
		canvas.createBufferStrategy(2);
		strategy = canvas.getBufferStrategy();
	}

	/**
	 * Returns the JFrame.
	 * @return the JFrame
	 */
	public JFrame getFrame() {
		return frame;
	}
	
	/**
	 * Returns the canvas.
	 * You actually write to the canvas, not to a JPanel or JFrame.
	 * The canvas is needed because only this supports BufferStrategy.
	 * A JPanel doesn't support this.
	 * @return the Canvas
	 */
	public Canvas getCanvas() {
		return canvas;
	}
	
	/**
	 * Returns the Graphics2D object from the BufferStrategy (the "screen" in memory).
	 * @return the Graphics2D
	 */
	public Graphics2D getGraphics2D(){
		return (Graphics2D) strategy.getDrawGraphics();
	}
	
	/**
	 * Flips/copies the "screen" in memory to the real screen.
	 */
	public void showStrategyGraphics(){
		strategy.show();
	}

	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent e) {
		
	}

	@Override
	public void windowClosing(WindowEvent e) {
		shooter.setGame(false);	
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent e) {
			
	}
}
